package qldc.pm.tienan.service.persistence;

import java.util.Date;
import java.util.Iterator;
import java.util.List;








import qldc.pm.qtht.dmdc.utils.StringUtils;
import qldc.pm.tienan.model.KetQuaTrichLuc;

import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.dao.orm.Type;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.dao.orm.CustomSQLUtil;

public class KetQuaTrichLucFinderImpl extends
BasePersistenceImpl<KetQuaTrichLuc> implements KetQuaTrichLucFinder {
	private static Log _log = LogFactoryUtil.getLog(KetQuaTrichLucFinderImpl.class);

public static final String FIND_DS_TRICH_LUC = KetQuaTrichLucFinder.class
	.getName() + ".findByDS_TrichLuc";

public static final String COUNT_DS_TRICH_LUC = KetQuaTrichLucFinder.class
	.getName() + ".countByDS_TrichLuc";

@SuppressWarnings("unchecked")
public List<Object[]> findByDS_TrichLuc(long tinhId, long quanHuyenId, long phuongXaId, Date tuNgayTrichLuc, Date denNgayTrichLuc, String coQuanTrichLuc, String nguoiThucHienTrichLuc)
	throws SystemException {

	Session session = null;

	try {
		session = openSession();


		String sql = CustomSQLUtil.get(FIND_DS_TRICH_LUC);

		if (Validator.isNull(coQuanTrichLuc)){
			sql = StringUtil.replace(sql, "AND TS_YeuCauTrichLuc.coQuanYeuCauTrichLuc like ?", StringPool.BLANK);
		}
		if (tinhId <= 0) {
			sql = StringUtil.replace(sql,
					"AND TS_YeuCauTrichLuc.noiThuongTruDoiTuongTrichLucTinhId = ?", StringPool.BLANK);
		}

		if (quanHuyenId <= 0) {
			sql = StringUtil.replace(sql,
					"AND TS_YeuCauTrichLuc.noiThuongTruDoiTuongTrichLucQuanHuyenId = ?",
					StringPool.BLANK);
		}

		if (phuongXaId <= 0) {
			sql = StringUtil.replace(sql,
					"AND TS_YeuCauTrichLuc.noiThuongTruDoiTuongTrichLucPhuongXaId = ?",
					StringPool.BLANK);
		}

		if (Validator.isNull(tuNgayTrichLuc)) {
			sql = StringUtil.replace(sql,
					"AND TS_KetQuaTrichLuc.ngayTraCuu >= ?", StringPool.BLANK);
		}

		if (Validator.isNull(denNgayTrichLuc)) {
			sql = StringUtil.replace(sql,
					"AND TS_KetQuaTrichLuc.ngayTraCuu <= ?", StringPool.BLANK);
		}
		if (Validator.isNull(nguoiThucHienTrichLuc)){
			sql = StringUtil.replace(sql, "AND TS_KetQuaTrichLuc.nguoiTraCuuHoSo like ?", StringPool.BLANK);
		}
		
		sql = CustomSQLUtil.replaceAndOperator(sql, true);
		SQLQuery q = session.createSQLQuery(sql);

		q.addScalar("coQuanYeuCauTrichLuc", Type.STRING);
		q.addScalar("hoVaTenDoiTuongTrichLuc", Type.STRING);
		q.addScalar("noiThuongTruDoiTuongTrichLucTinhId", Type.LONG);
		q.addScalar("noiThuongTruDoiTuongTrichLucQuanHuyenId", Type.LONG);
		q.addScalar("noiThuongTruDoiTuongTrichLucPhuongXaId", Type.LONG);
		q.addScalar("noiThuongTruDoiTuongTrichLucDiaChi", Type.STRING);
		q.addScalar("ngayTraCuu", Type.STRING);
		q.addScalar("nguoiTraCuuHoSo", Type.STRING);
		q.addScalar("soDinhDanhCaNhanDoiTuongTrichLuc", Type.STRING);
		q.addScalar("soCMNDDoiTuongTrichLuc", Type.STRING);
		q.addScalar("ngaySinhDoiTuongTrichLuc", Type.STRING);
		q.addScalar("ketQua", Type.INTEGER);

		QueryPos qpos = QueryPos.getInstance(q);

		if (Validator.isNotNull(coQuanTrichLuc)) {
			qpos.add(StringUtils.getLike(coQuanTrichLuc));
		}
		

		if (tinhId > 0) {
			qpos.add(tinhId);
		}

		if (quanHuyenId > 0) {
			qpos.add(quanHuyenId);
		}

		if (phuongXaId > 0) {
			qpos.add(phuongXaId);
		}

	
		if (Validator.isNotNull(tuNgayTrichLuc)) {
			qpos.add(tuNgayTrichLuc);
		}

		if (Validator.isNotNull(denNgayTrichLuc)) {
			qpos.add(denNgayTrichLuc);
		}
		if (Validator.isNotNull(nguoiThucHienTrichLuc)) {
			qpos.add(StringUtils.getLike(nguoiThucHienTrichLuc));
		}
		

		return (List<Object[]>) QueryUtil.list(q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
	} catch (Exception e) {
		throw new SystemException(e);
	} finally {
		closeSession(session);
	}

}


@SuppressWarnings("unchecked")
public int countDS_TrichLuc(long tinhId, long quanHuyenId, long phuongXaId, Date tuNgayTrichLuc, Date denNgayTrichLuc, String coQuanTrichLuc, String nguoiThucHienTrichLuc)
	throws SystemException {

	Session session = null;

	try {
		session = openSession();


		String sql = CustomSQLUtil.get(COUNT_DS_TRICH_LUC);

		if (Validator.isNull(coQuanTrichLuc)){
			sql = StringUtil.replace(sql, "AND TS_YeuCauTrichLuc.coQuanYeuCauTrichLuc like ?", StringPool.BLANK);
		}
		if (tinhId <= 0) {
			sql = StringUtil.replace(sql,
					"AND TS_YeuCauTrichLuc.noiThuongTruDoiTuongTrichLucTinhId = ?", StringPool.BLANK);
		}

		if (quanHuyenId <= 0) {
			sql = StringUtil.replace(sql,
					"AND TS_YeuCauTrichLuc.noiThuongTruDoiTuongTrichLucQuanHuyenId = ?",
					StringPool.BLANK);
		}

		if (phuongXaId <= 0) {
			sql = StringUtil.replace(sql,
					"AND TS_YeuCauTrichLuc.noiThuongTruDoiTuongTrichLucPhuongXaId = ?",
					StringPool.BLANK);
		}

		if (Validator.isNull(tuNgayTrichLuc)) {
			sql = StringUtil.replace(sql,
					"AND TS_KetQuaTrichLuc.ngayTraCuu >= ?", StringPool.BLANK);
		}

		if (Validator.isNull(denNgayTrichLuc)) {
			sql = StringUtil.replace(sql,
					"AND TS_KetQuaTrichLuc.ngayTraCuu <= ?", StringPool.BLANK);
		}
		if (Validator.isNull(nguoiThucHienTrichLuc)){
			sql = StringUtil.replace(sql, "AND TS_KetQuaTrichLuc.nguoiTraCuuHoSo like ?", StringPool.BLANK);
		}
		
		sql = CustomSQLUtil.replaceAndOperator(sql, true);

		SQLQuery q = session.createSQLQuery(sql);

		q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

		QueryPos qpos = QueryPos.getInstance(q);
		


		if (Validator.isNotNull(coQuanTrichLuc)) {
			qpos.add(StringUtils.getLike(coQuanTrichLuc));
		}
		

		if (tinhId > 0) {
			qpos.add(tinhId);
		}

		if (quanHuyenId > 0) {
			qpos.add(quanHuyenId);
		}

		if (phuongXaId > 0) {
			qpos.add(phuongXaId);
		}

	
		if (Validator.isNotNull(tuNgayTrichLuc)) {
			qpos.add(tuNgayTrichLuc);
		}

		if (Validator.isNotNull(denNgayTrichLuc)) {
			qpos.add(denNgayTrichLuc);
		}
		if (Validator.isNotNull(nguoiThucHienTrichLuc)) {
			qpos.add(StringUtils.getLike(nguoiThucHienTrichLuc));
		}
		
		Iterator<Long> iterator = q.list().iterator();
		if (iterator.hasNext()) {
			Long count = iterator.next();

			if (Validator.isNotNull(count)) {
				return count.intValue();
			}
		}
	} catch (Exception e) {
		throw new SystemException(e);
	} finally {
		closeSession(session);
	}
	return 0;

}

}
